package cn.chinaflame.pagodas.utils.LightAlgo;

import java.util.List;

public class LightSearch {
    private static final boolean leftSearch = true;//默认从左侧开始寻灯点灯
    private static final boolean topSearch = true;//默认从上往下开始寻灯点灯
//    private static List<String> lightnos;
    // static{
    //    lightnos = LightUtil.genLightno(row, column);
    //}


    /**
     * 从左到右，从上到下逐行寻灯
     *
     * @param vertexs
     */
    public static synchronized String leftdownSearch(List<Vertex> vertexs) {
        if (vertexs == null || vertexs.size() == 0) return "";
        Vertex vex = null;
        for (Vertex vertex :
                vertexs) {
            if (!vertex.isLighted()) {
                vertex.setLighted(true);
                vex = vertex;
                break;
            }
        }
        return vex != null ? vex.getLightno() : "";
    }

    /**
     * 从右到左，从下到上逐行寻灯
     *
     * @param vertexs
     */
    public static synchronized String rightupSearch(List<Vertex> vertexs) {
        if (vertexs == null || vertexs.size() == 0) return "";
        Vertex vex = null;
        for (int i = vertexs.size() - 1; i >= 0; i--) {
            if (vertexs.get(i) != null && !vertexs.get(i).isLighted()) {
                vertexs.get(i).setLighted(true);
                vex = vertexs.get(i);
                break;
            }
        }
        return vex != null ? vex.getLightno() : "";
    }

    /**
     * 先从中间行左侧开始寻灯，该行结束先上后下逐行寻灯
     *
     * @param row
     * @param column
     */
    public static Vertex midCenterUpDown(Integer area, Integer seat, Integer pillar, Integer row, Integer column) {
        int midRow = (row + 1) / 2;//中间行的行号
        int rowup = midRow - 1;//上面行的行数
        int rowdown = row - midRow;//下面行的行数
        //String lightno = LightUtil.findLightno(area,seat,pillar,midRow,1);//中间行左侧灯号
        List<List<Vertex>> vertexsList = LightUtil.genLightMatrix(area, seat, pillar, midRow, column);
        int maxrow = Math.max(rowup, rowdown);
        List<Vertex> vertexs = vertexsList.get(midRow);
        for (Vertex v : vertexs) {
            if (!v.isLighted())
                return v;
        }
        for (int i = 1; i <= maxrow; i++) {
            List<Vertex> upVertexs = vertexsList.get(midRow - i);
            List<Vertex> downVertexs = vertexsList.get(midRow + i);
            for (int j = 1; j <= column; j++) {//先在上面的行寻灯，若寻不着，则去下面的行寻灯
                if (upVertexs != null && upVertexs.size() > 0 && !upVertexs.get(j).isLighted())
                    return upVertexs.get(j);
            }
            for (int j = 1; j <= column; j++) {
                if (downVertexs != null && downVertexs.size() > 0 && !downVertexs.get(j).isLighted())
                    return downVertexs.get(j);
            }
        }
        return null;
    }

    /**
     * 先从中间行左侧开始寻灯，该行结束先下后上逐行寻灯
     *
     * @param row
     * @param column
     */
    public static Vertex midCenterDownUp(Integer area, Integer seat, Integer pillar, Integer row, Integer column) {
        int midRow = (row + 1) / 2;//中间行的行号
        int rowup = midRow - 1;//上面行的行数
        int rowdown = row - midRow;//下面行的行数
        // String lightno = LightUtil.findLightno(area,seat,pillar,midRow,1);//中间行左侧灯号
        List<List<Vertex>> vertexsList = LightUtil.genLightMatrix(area, seat, pillar, midRow, column);
        int maxrow = Math.max(rowup, rowdown);
        List<Vertex> vertexs = vertexsList.get(midRow);
        for (Vertex v : vertexs) {
            if (!v.isLighted())
                return v;
        }
        for (int i = 1; i <= maxrow; i++) {
            List<Vertex> upVertexs = vertexsList.get(midRow - i);
            List<Vertex> downVertexs = vertexsList.get(midRow + i);
            for (int j = 1; j <= column; j++) {//先在下面的行寻灯，若寻不着，则去上面的行寻灯
                if (downVertexs != null && downVertexs.size() > 0 && !downVertexs.get(j).isLighted())
                    return downVertexs.get(j);
            }
            for (int j = 1; j <= column; j++) {
                if (upVertexs != null && upVertexs.size() > 0 && !upVertexs.get(j).isLighted())
                    return upVertexs.get(j);
            }
        }
        return null;
    }


    /**
     * 先从中间行右侧开始寻灯，该行结束先上后下逐行寻灯，寻灯顺序是从右往左
     *
     * @param row
     * @param column
     */
    public static Vertex reverseMidCenterUpDown(Integer area, Integer seat, Integer pillar, Integer row, Integer column) {
        int midRow = (row + 1) / 2;//中间行的行号
        int rowup = midRow - 1;//上面行的行数
        int rowdown = row - midRow;//下面行的行数
        //  String lightno = LightUtil.findLightno(area,seat,pillar,midRow,1);//中间行左侧灯号
        List<List<Vertex>> vertexsList = LightUtil.genLightMatrix(area, seat, pillar, midRow, column);
        int maxrow = Math.max(rowup, rowdown);
        List<Vertex> vertexs = vertexsList.get(midRow);
        for (int k = vertexs.size(); k > 0; k--) {
            if (vertexs != null && vertexs.size() > 0 && !vertexs.get(k).isLighted())
                return vertexs.get(k);
        }
        for (int i = 1; i <= maxrow; i++) {
            List<Vertex> upVertexs = vertexsList.get(midRow - i);
            List<Vertex> downVertexs = vertexsList.get(midRow + i);
            for (int j = column; j > 0; j--) {//先在上面的行寻灯，若寻不着，则去上面的行寻灯，寻灯顺序是从右往左
                if (upVertexs != null && upVertexs.size() > 0 && !upVertexs.get(j).isLighted())
                    return upVertexs.get(j);
            }
            for (int j = column; j > 0; j--) {
                if (downVertexs != null && downVertexs.size() > 0 && !downVertexs.get(j).isLighted())
                    return downVertexs.get(j);
            }
        }
        return null;
    }


    /**
     * 先从中间行右侧开始寻灯，该行结束先上后下逐行寻灯，寻灯顺序是从右往左
     *
     * @param row
     * @param column
     */
    public static Vertex reverseMidCenterDownUp(Integer area, Integer seat, Integer pillar, Integer row, Integer column) {
        int midRow = (row + 1) / 2;//中间行的行号
        int rowup = midRow - 1;//上面行的行数
        int rowdown = row - midRow;//下面行的行数
        //String lightno = LightUtil.findLightno(area,seat,pillar,midRow,1);//中间行左侧灯号
        List<List<Vertex>> vertexsList = LightUtil.genLightMatrix(area, seat, pillar, midRow, column);
        int maxrow = Math.max(rowup, rowdown);
        List<Vertex> vertexs = vertexsList.get(midRow);
        for (int k = vertexs.size(); k > 0; k--) {
            if (vertexs != null && vertexs.size() > 0 && !vertexs.get(k).isLighted())
                return vertexs.get(k);
        }
        for (int i = 1; i <= maxrow; i++) {
            List<Vertex> upVertexs = vertexsList.get(midRow - i);
            List<Vertex> downVertexs = vertexsList.get(midRow + i);
            for (int j = column; j > 0; j--) {//先在下面的行寻灯，若寻不着，则去上面的行寻灯，寻灯顺序是从右往左
                if (downVertexs != null && downVertexs.size() > 0 && !downVertexs.get(j).isLighted())
                    return downVertexs.get(i);
            }
            for (int j = column; j > 0; j--) {
                if (upVertexs != null && upVertexs.size() > 0 && !upVertexs.get(j).isLighted())
                    return upVertexs.get(i);
            }
        }
        return null;
    }

    /**
     * 先从中间行中间点开始寻灯，该点左侧从右向左寻灯，该点右侧从左向右寻灯，右向寻灯优先
     *
     * @param row
     * @param column
     */
    public static Vertex midTwoHeadDownFirstSearch(Integer area, Integer seat, Integer pillar, Integer row, Integer column) {
        int midRow = (row + 1) / 2;//中间行的行号
        Integer midcol = (column + 1) / 2;//中间列
        int rowup = midRow - 1;//上面行的行数
        int rowdown = row - midRow;//下面行的行数
        int maxrow = Math.max(rowup, rowdown);
        String lightno = LightUtil.findLightno(area, seat, pillar, midRow, midcol);//中间行左侧灯号
        List<String> lightnos = LightUtil.genLightno(area, seat, pillar, row, column);
        List<List<Vertex>> vertexsList = LightUtil.genLightMatrix(area, seat, pillar, midRow, column);
        List<Vertex> midrowList = vertexsList.get(midRow);
        if (!midrowList.get(midcol).isLighted()) return midrowList.get(midcol);
        List<Vertex> leftList = midrowList.subList(0, midcol);
        List<Vertex> rightList = midrowList.subList(midcol + 1, column - 1);
        int maxcol = Math.max(leftList.size(), rightList.size());
        for (int i = 0; i < maxcol; i++) {
            if (rightList != null && rightList.get(i) != null && !rightList.get(i).isLighted()) return rightList.get(i);
            if (leftList != null && leftList.get(leftList.size() - i - 1) != null && !leftList.get(leftList.size() - i - 1).isLighted())
                return leftList.get(leftList.size() - i - 1);
        }
        for (int i = 1; i <= maxrow; i++) {
            List<Vertex> upVertexs = vertexsList.get(midRow - i);
            List<Vertex> downVertexs = vertexsList.get(midRow + i);
            for (int j = 0; j < column; j++) {//先在下面的行寻灯，寻灯顺序是从左向右，若寻不着，则去上面的行寻灯，寻灯顺序是从右往左
                if (downVertexs != null && downVertexs.size() > 0 && !downVertexs.get(j).isLighted())
                    return downVertexs.get(j);
            }
            for (int j = column; j > 0; j--) {
                if (upVertexs != null && upVertexs.size() > 0 && !upVertexs.get(j).isLighted())
                    return upVertexs.get(j);
            }
        }
        return null;
    }

    /**
     * 先从中间行中间开始寻灯，该点左侧从右向左寻灯，该点右侧从左向右寻灯，左向寻灯优先
     *
     * @param row
     * @param column
     */
    public static Vertex midTwoHeadUpFirstSearch(Integer area, Integer seat, Integer pillar, Integer row, Integer column) {
        int midRow = (row + 1) / 2;//中间行的行号
        Integer midcol = (column + 1) / 2;//中间列
        int rowup = midRow - 1;//上面行的行数
        int rowdown = row - midRow;//下面行的行数
        int maxrow = Math.max(rowup, rowdown);
        String lightno = LightUtil.findLightno(area, seat, pillar, midRow, midcol);//中间行左侧灯号
        List<String> lightnos = LightUtil.genLightno(area, seat, pillar, row, column);
        List<List<Vertex>> vertexsList = LightUtil.genLightMatrix(area, seat, pillar, midRow, column);
        List<Vertex> midrowList = vertexsList.get(midRow);
        if (!midrowList.get(midcol).isLighted()) return midrowList.get(midcol);
        List<Vertex> leftList = midrowList.subList(0, midcol);
        List<Vertex> rightList = midrowList.subList(midcol + 1, column - 1);
        int maxcol = Math.max(leftList.size(), rightList.size());
        for (int i = 0; i < maxcol; i++) {
            if (leftList != null && leftList.get(leftList.size() - i - 1) != null && !leftList.get(leftList.size() - i - 1).isLighted())
                return leftList.get(leftList.size() - i - 1);
            if (rightList != null && rightList.get(i) != null && !rightList.get(i).isLighted()) return rightList.get(i);
        }
        for (int i = 1; i <= maxrow; i++) {
            List<Vertex> upVertexs = vertexsList.get(midRow - i);
            List<Vertex> downVertexs = vertexsList.get(midRow + i);
            for (int j = column; j > 0; j--) {//先在上面的行寻灯，寻灯顺序是从右向左，若寻不着，则去下面的行寻灯，寻灯顺序是从左向右
                if (upVertexs != null && upVertexs.size() > 0 && !upVertexs.get(j).isLighted())
                    return upVertexs.get(j);
            }
            for (int j = 0; j < column; j++) {
                if (downVertexs != null && downVertexs.size() > 0 && !downVertexs.get(j).isLighted())
                    return downVertexs.get(j);
            }
        }
        return null;
    }
}
